//
// Created by liu on 2020/10/28.
//

#include "config.h"

namespace alg{


    int Config::parse_config(std::string const& conf_path){

        Json::Reader reader;
        Json::Value root;

        std::ifstream in(conf_path, std::ios::binary);

        if (!in.is_open())
        {
            std::cout << "Error opening file\n";
            return -1;
        }

        if (reader.parse(in, root))
        {

            // homework detect
            str_conf_.emplace(make_pair("det_model_path",root["homework"]["det_model_path"].asString()));
            str_conf_.emplace(make_pair("det_cfg_path",root["homework"]["det_cfg_path"].asString()));
            str_conf_.emplace(make_pair("coco_names_path",root["homework"]["coco_names_path"].asString()));

            //detect type
            int_conf_.emplace(std::make_pair("detect_type",root["detect_type"].asInt()));

            //calc_detectparmameter
            int_conf_.emplace(std::make_pair("minAdaThreshold",root["Calc_DetctParameter"]["minAdaThreshold"].asInt()));
            int_conf_.emplace(std::make_pair("maxAdaThreshold",root["Calc_DetctParameter"]["maxAdaThreshold"].asInt()));
            int_conf_.emplace(std::make_pair("random_sample",root["Calc_DetctParameter"]["random_sample"].asInt()));
            int_conf_.emplace(std::make_pair("num_samples",root["Calc_DetctParameter"]["num_samples"].asInt()));
            int_conf_.emplace(std::make_pair("num_min_matches",root["Calc_DetctParameter"]["num_min_matches"].asInt()));
            int_conf_.emplace(std::make_pair("frameCount",root["Calc_DetctParameter"]["frameCount"].asInt()));
            int_conf_.emplace(std::make_pair("step",root["Calc_DetctParameter"]["step"].asInt()));
            int_conf_.emplace(std::make_pair("thresh_binary",root["Calc_DetctParameter"]["thresh_binary"].asInt()));
            float_conf_.emplace(std::make_pair("Calc_iouThreshold",root["Calc_DetctParameter"]["iouThreshold"].asDouble()));

            //sort
            int_conf_.emplace(std::make_pair("maxMissFrame",root["SortParameter"]["maxMissFrame"].asInt()));
            int_conf_.emplace(std::make_pair("min_hits",root["SortParameter"]["min_hits"].asInt()));
            int_conf_.emplace(std::make_pair("max_age",root["SortParameter"]["max_age"].asInt()));


            //onnx
            str_conf_.emplace(make_pair("model_path",root["OnnxParam"]["model_path"].asString()));


            int_conf_.emplace(make_pair("net_input_batch",root["OnnxParam"]["net_input_batch"].asInt()));
            int_conf_.emplace(make_pair("net_input_channel",root["OnnxParam"]["net_input_channel"].asInt()));



            int_conf_.emplace(make_pair("net_input_height",root["OnnxParam"]["net_input_height"].asInt()));
            int_conf_.emplace(make_pair("net_input_width",root["OnnxParam"]["net_input_width"].asInt()));
            str_conf_.emplace(make_pair("net_input_means",root["OnnxParam"]["net_input_means"].asString()));
            str_conf_.emplace(make_pair("net_input_norm",root["OnnxParam"]["net_input_norm"].asString()));

            str_conf_.emplace(make_pair("featurem_map_size",root["OnnxParam"]["featurem_map_size"].asString()));
            str_conf_.emplace(make_pair("shrinkage",root["OnnxParam"]["shrinkage"].asString()));
            str_conf_.emplace(make_pair("boxsizes",root["OnnxParam"]["boxsizes"].asString()));
            str_conf_.emplace(make_pair("aspect_ratios",root["OnnxParam"]["aspect_ratios"].asString()));
            int_conf_.emplace(make_pair("num_class",root["OnnxParam"]["num_class"].asInt()));
            float_conf_.emplace(make_pair("center_variance",root["OnnxParam"]["center_variance"].asDouble()));
            float_conf_.emplace(make_pair("size_variance",root["OnnxParam"]["size_variance"].asDouble()));
            float_conf_.emplace(make_pair("score_threshold",root["OnnxParam"]["score_threshold"].asDouble()));
            float_conf_.emplace(make_pair("Onnx_iou_threshold",root["OnnxParam"]["iou_threshold"].asDouble()));


            //logic
            int_conf_.emplace(make_pair("MinWidth",root["logicParam"]["MinWidth"].asInt()));
            int_conf_.emplace(make_pair("MinHeight",root["logicParam"]["MinHeight"].asInt()));

            float_conf_.emplace(make_pair("C1",root["logicParam"]["C1"].asDouble()));
            float_conf_.emplace(make_pair("C2",root["logicParam"]["C2"].asDouble()));




        }
        else
        {
            cout << "parse error\n" << endl;
        }
        in.close();



        return 0;
    }

    std::vector<int> Config::Split(const std::string& src, const std::string& separator)
    {
        std::vector<string> ss;
        std::vector<int> dest;

        string::size_type pos1,pos2;
        pos2 = src.find(separator);
        pos1 = 0 ;
        while(string::npos !=pos2){
            ss.push_back(src.substr(pos1,pos2-pos1));
            pos1 = pos2 + separator.size();
            pos2 = src.find(separator,pos1);
        }
        if(pos1!=src.length()){
            ss.push_back(src.substr(pos1));
        }
        for(auto s:ss){
            dest.push_back(atoi(s.data()));
        }

        return dest;
    }

}
